package com.powergisol.gis.service.impl;

import com.powergisol.gis.dao.TabDataSettingMapper;
import com.powergisol.gis.dao.TabPipeStaticColorMapper;
import com.powergisol.gis.entity.TabDataSetting;
import com.powergisol.gis.entity.TabPipeStaticColor;
import com.powergisol.gis.exception.GlobalExceptionHandler;
import com.powergisol.gis.service.TabPipeStaticColorService;
import com.powergisol.gis.util.Result;
import com.powergisol.gis.vo.PipeStaticColorVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TabPipeStaticColorServiceImpl implements TabPipeStaticColorService {
    @Autowired
    TabPipeStaticColorMapper tabPipeStaticColorMapper;
    @Autowired
    TabDataSettingMapper settingMapper;

    @Override
    public boolean deleteByPrimaryKey(Integer id) {
        return tabPipeStaticColorMapper.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public Result insertBatch(PipeStaticColorVO vo) {

        try {
            TabDataSetting byServiceId = settingMapper.getOneByUuid(vo.getUuid());
            if (byServiceId == null) {
                new GlobalExceptionHandler("服务不存在");
            }
            vo.getColors().forEach(c -> {
                c.setSettingId(byServiceId.getId());
            });
            List<TabPipeStaticColor> colors = tabPipeStaticColorMapper.selectAll(byServiceId.getId());
            if(colors!=null && colors.size()>0){
                updateByServiceId(vo.getColors());
            }else{
                tabPipeStaticColorMapper.insertBatch(vo.getColors());
            }
        } catch (Exception e) {
            throw new GlobalExceptionHandler(500, e.getMessage());
        }
        return new Result().success("管网颜色设置成功");
    }

    @Override
    public boolean insertSelective(TabPipeStaticColor record) {
        return tabPipeStaticColorMapper.insertSelective(record) > 0;
    }

    @Override
    public TabPipeStaticColor selectByPrimaryKey(Integer id) {
        return tabPipeStaticColorMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean updateByPrimaryKeySelective(TabPipeStaticColor record) {
        return tabPipeStaticColorMapper.updateByPrimaryKeySelective(record) > 0;
    }

    @Override
    public boolean updateByPrimaryKey(TabPipeStaticColor record) {
        return tabPipeStaticColorMapper.updateByPrimaryKey(record) > 0;
    }

    @Override
    public Result selectAll(String uuid) {
        TabDataSetting oneByUuid = settingMapper.getOneByUuid(uuid);
        List<TabPipeStaticColor> colors = tabPipeStaticColorMapper.selectAll(oneByUuid.getId());
        Map<String, List<TabPipeStaticColor>> collect = colors.stream().collect(Collectors.groupingBy(TabPipeStaticColor::getDistinguish));
        return new Result().success(collect);
    }

    @Override
    public List<TabPipeStaticColor> selectBySettingId(Integer serviceId) {
        TabDataSetting byServiceId = settingMapper.getByServiceId(serviceId);
        return settingMapper.selectBySettingId(byServiceId.getId());
    }

    @Override
    public Result updateByServiceId(List<TabPipeStaticColor> staticColors) {
        staticColors.forEach(c->{
            tabPipeStaticColorMapper.updateByPrimaryKey(c);
        });
        return null;
    }
}
